<template>
<div style="display: flex;width: 100%;">
  <div>
    <JInput v-model="modelValue" :readonly="readonly" :disabled="disabled" :height="height" />
  </div>
  <div style="margin-left: 5px;">
    <JButton  ref="btnRef" size="small" @click="chose" :disabled="disabled">票据</JButton >
  </div>
  <j-dialog v-model="show" draggable style="width: 850px;" :title="tp===150501?'预付抵扣':'预收抵扣'">
    <j-grid :fields="cols" v-model="list" :height="500" :show-page="false" />
    <div style=" margin-top: 10px;">
      <JButton   type="info" @click="confirm" style="height: 40px;width: 80px;">确定</JButton >
    </div>
  </j-dialog>
</div>
</template>
<script setup>
import tool from "@/utils/tool";
import {ref, computed} from "vue";
const props = defineProps({
  disabled: {type: Boolean, default: false}, // 是否搜索模式
  readonly: {type: Boolean, default: true}, // 是否搜索模式
  height: {type: [String, Number], default: ''},
})
const emit = defineEmits(['change'])
const modelValue = defineModel()
const amount = ref(0)
const list = ref([])
const show = ref(false)
const fApi = ref({})
const tp = ref('')

const cols = computed(() => {
  let co = [
    {type: 'seq', width: 50},
    {field: 'buName', title: tp.value===150501? '供应商':'客户', width: 220,},
    {field: 'billDate', title: '日期', width: 100,align: 'center'},
    {field: 'transTypeName', title: '类型', width: 100,align: 'center'},
    {field: 'billNo', title: '票号', width: 100, canClick: true},
    {field: 'leftAmount', title: tp.value===150501? '预付结余':'预收结余', width: 120, align: 'right', sum: true},
    {field: 'hxAmount', title: '本次核销', width: 100, align: 'right', sum: true,editRender: { name: '$input', props: { type: 'number'} }},
  ]
  return co
})
const transType = computed(() => {
  return  fApi.value.getValue('transType')
})
const setApi = (api) => {
  fApi.value = api
}
const cellClick = ({row}) => {
  row.checked = !row.checked
}
// 收付款选择源单
const chose = () => {
  list.value = []
  let buId = 0
  let transType = 0
  let billId = 0
  if(tool.xe.isEmpty(fApi.value)) {
    buId = this.buId
    transType = transType.value
    billId = this.billId
  }
  else {
    buId = fApi.value.getValue('buId')
    transType = fApi.value.getValue('transType')
    billId = fApi.value.getValue('id')
    tp.value = transType
  }
  if(!buId) return tool.err('没有选择' + (transType.value === 150601 ?'客户':'供应商'))
  if(!transType) return tool.err('业务类型不明确')
  tool.post('bill/getYsfList', {
    billId: billId,
    buId: buId,
    transType: transType
  }).then(ret => {
    if(ret.stat) {
      show.value = true
      list.value = ret.data
    }
  })
}
const confirm = () => {
  let list = list.value.filter(row => {
    row.hxAmount = tool.xe.toNumber(row.hxAmount)
    row.leftAmount = tool.xe.toNumber(row.leftAmount)
    return row.hxAmount !== 0
  })
  for (let i = 0; i < list.length; i++) {
    if(list[i].leftAmount > 0) {
      if(list[i].hxAmount<0) {
        return tool.err('本次核销不能小于0')
      }
      if(list[i].hxAmount > list[i].leftAmount) {
        return tool.err(`第${i+1}行 核销金额大于结余`)
      }
    }
    else {
      if(list[i].hxAmount>0) {
        return tool.err('本次核销不能大于0')
      }
      if(list[i].hxAmount < list[i].leftAmount) {
        return tool.err(`第${i+1}行 核销金额大于结余`)
      }
    }
  }
  let amount = tool.xe.sum(list, 'hxAmount')
  setTimeout(()=>{
     emit('change', {
      amount: amount,
      list: list
    })
  },1)
  show.value = false
}

defineExpose({setApi})
</script>